package xyz.jcat.biz.admin.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.jcat.biz.admin.domain.dto.UserAddDto;
import xyz.jcat.biz.admin.domain.dto.UserRoleBindDto;
import xyz.jcat.biz.admin.domain.vo.UserInfoVo;
import xyz.jcat.biz.admin.service.IUserRoleService;
import xyz.jcat.biz.admin.service.IUserService;
import xyz.jcat.common.auth.UserContextHolder;
import xyz.jcat.common.web.Resp;
import xyz.jcat.web.security.annotation.Menu;
import xyz.jcat.web.security.annotation.RequiresPermission;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
@Menu(name = "用户管理", value = "user_manage")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IUserRoleService userRoleService;

    @GetMapping("/{id}")
    @RequiresPermission(name = "查询用户", value = "user:get")
    public Resp getUser(@PathVariable Long id) {
        UserInfoVo vo = userService.getUserInfo(id);
        return Resp.ok(vo);
    }

    @PostMapping
    @RequiresPermission(name = "添加用户", value = "user:add")
    public Resp addUser(@Valid @RequestBody UserAddDto userAddDto) {
        userService.addUser(userAddDto);
        return Resp.ok();
    }

    @GetMapping
    public Resp getLoginUserInfo() {
        UserInfoVo vo = userService.getUserInfo(UserContextHolder.getInstance().getUserId());
        return Resp.ok(vo);
    }

    @PostMapping("/role")
    @RequiresPermission(name = "绑定角色", value = "user:role:bind")
    public Resp bindUserRole(@Valid @RequestBody UserRoleBindDto userRoleBindDto) {
        userRoleService.bindUserRole(userRoleBindDto);
        return Resp.ok();
    }
}
